//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.comprosoft.utils;

import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.Kernel32;
import com.sun.jna.platform.win32.WinNT.HANDLE;
import java.lang.reflect.Field;

public class ProcessUtils {
    public ProcessUtils() {
    }

    public static void killProcessTree(Process process) {
        try {
            Field f = process.getClass().getDeclaredField("handle");
            f.setAccessible(true);
            long handl = f.getLong(process);
            Kernel32 kernel = Kernel32.INSTANCE;
            HANDLE handle = new HANDLE();
            handle.setPointer(Pointer.createConstant(handl));
            int ret = kernel.GetProcessId(handle);
            Long PID = (long)ret;
            String cmd = getKillProcessTreeCmd(PID);
            Runtime rt = Runtime.getRuntime();
            Process killPrcess = rt.exec(cmd);
            killPrcess.waitFor();
            killPrcess.destroy();
        } catch (Exception var11) {
            var11.printStackTrace();
        }

    }

    private static String getKillProcessTreeCmd(Long Pid) {
        String result = "";
        if (Pid != null) {
            result = "cmd.exe /c taskkill /PID " + Pid + " /F /T ";
        }

        return result;
    }

    public static Long getPid(Process process) {
        Long pid = -1L;

        try {
            Field f = process.getClass().getDeclaredField("handle");
            f.setAccessible(true);
            long handl = f.getLong(process);
            Kernel32 kernel = Kernel32.INSTANCE;
            HANDLE handle = new HANDLE();
            handle.setPointer(Pointer.createConstant(handl));
            int ret = kernel.GetProcessId(handle);
            pid = (long)ret;
        } catch (Exception var8) {
            var8.printStackTrace();
        }

        return pid;
    }

    public static void killProcessTree(long pid) {
        try {
            String cmd = getKillProcessTreeCmd(pid);
            Runtime rt = Runtime.getRuntime();
            Process killPrcess = rt.exec(cmd);
            killPrcess.waitFor();
            killPrcess.destroy();
        } catch (Exception var5) {
            var5.printStackTrace();
        }

    }
}
